package com.itheima.reggie.controller;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 去支付，生成订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("提交订单成功");
    }

    @GetMapping("/userPage")
    public R queryPageOrders(int page,int pageSize){
        IPage<Orders> p = new Page(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
         ordersService.page(p);
        return  R.success(p);
    }

    /**
     * 后端分页查询
     * @param page
     * @param pageSize
     * @param beginTime
     * @param endTime
     * @param number
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    public R queryPageBackendOrders(int page, int pageSize, String beginTime, String endTime,Long number) throws Exception {
        IPage<Orders> p = new Page(page,pageSize);
        //查询所有order表信息
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
      //  queryWrapper.gt(beginTime!=null,Orders::getOrderTime, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beginTime));
       // queryWrapper.lt(endTime!=null,Orders::getOrderTime,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(beginTime!=null){
            Date beginTime1 = simpleDateFormat.parse(beginTime);
            queryWrapper.ge(Orders::getOrderTime, beginTime1);
            //queryWrapper.apply("UNIX_TIMESTAMP(order_time) >= UNIX_TIMESTAMP('" + beginTime + "')");
        }
        if(endTime != null){
            Date endTime1 = simpleDateFormat.parse(endTime);
            queryWrapper.le(Orders::getOrderTime, endTime1);
           // queryWrapper.apply("UNIX_TIMESTAMP(order_time) < UNIX_TIMESTAMP('" + endTime + "')");
        }
        if(number != null){
            queryWrapper.like(Orders::getId,number);
        }

        ordersService.page(p,queryWrapper);

        return  R.success(p);
    }

    /**
     * 派送
     * @param orders
     * @return
     */
    @PutMapping
    public R delivery(@RequestBody Orders orders){
        System.out.println(orders.toString());
        LambdaUpdateWrapper<Orders> qw = new LambdaUpdateWrapper<>();
        qw.set(orders.getStatus()!=null,Orders::getStatus,orders.getStatus()).
                eq(orders.getId()!=null,Orders::getId,orders.getId());
        boolean flag = ordersService.update(qw);
        return flag?R.success("派送成功"):R.error("派送失败");
    }


}
